﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Nava_Manage : System.Web.UI.Page
{
    public int NavaParentID
    {
        get { return Convert.ToInt32(ViewState["_NavaParentID"]); }
        set { ViewState["_NavaParentID"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            NavaParentID = DongDong.Common.Misc.GetQueryStringAsInt("NavaParentID");
            PopulateNavagation();
        }
    }

    public void PopulateNavagation()
    {
        lvNavagation.DataSource = Nava.GetAllByParentID_TypeID(NavaParentID, (int)Nava.type.培训);
        lvNavagation.DataBind();
    }

    protected void lvNavagation_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lvNavagation.EditIndex = -1;
        btnSerach_Click(null, null);

    }

    protected void lvNavagation_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        lvNavagation.EditIndex = e.NewEditIndex;
        btnSerach_Click(null, null);
        int ID = DongDong.Common.Misc.ConvertToInt(lvNavagation.DataKeys[e.NewEditIndex].Value);
        Nava objNavigation = Nava.Get(ID);
        DropDownList drpParent = (DropDownList)lvNavagation.Items[e.NewEditIndex].FindControl("drpParent");
        Nava objPare = Nava.Get(objNavigation.ParentID);
        if (objPare != null)
        {
            drpParent.DataSource = Nava.GetAll().Where(row => row.ParentID == objPare.ParentID);
            drpParent.DataTextField = "Title";
            drpParent.DataValueField = "ID";
            drpParent.DataBind();
            drpParent.SelectedValue = objNavigation.ParentID.ToString();
        }
        else
        {
            drpParent.Items.Insert(0, new ListItem("请选择", "0"));
        }
    }
    protected void lvNavagation_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        int ID = DongDong.Common.Misc.ConvertToInt(lvNavagation.DataKeys[e.ItemIndex].Value);
        Nava.Delete(ID);
        btnSerach_Click(null, null);
    }

    protected void lvNavagation_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        int ID = DongDong.Common.Misc.ConvertToInt(lvNavagation.DataKeys[e.ItemIndex].Value);
        Nava objNava = Nava.Get(ID);
        var item = lvNavagation.Items[e.ItemIndex];

        TextBox txtEdiTitle = (TextBox)item.FindControl("txtEdiTitle");
        CheckBox cbEditKey = (CheckBox)item.FindControl("cbEditKey");
        FileUpload fulEditImg = (FileUpload)item.FindControl("fulEditImg");
        objNava.Title = txtEdiTitle.Text;

        objNava.Update();
        lvNavagation.EditIndex = -1;
        btnSerach_Click(null, null);
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Nava objNavigation = new Nava();
        objNavigation.Title = txtAddName.Text;
        objNavigation.RootID = NavaParentID;
        if (NavaParentID != 0)
        {
            Nava objNava = Nava.Get(NavaParentID);
            if (objNava.ParentID != 0)
                objNavigation.RootID = objNava.RootID;
        }

        objNavigation.Type = (int)Nava.type.培训;
        objNavigation.ParentID = NavaParentID;
        objNavigation.Add();
        btnSerach_Click(null, null);
    }

    protected void btnSerach_Click(object sender, EventArgs e)
    {

        lvNavagation.DataSource = Nava.GetAllByParentID_TypeID(NavaParentID, (int)Nava.type.培训);
        lvNavagation.DataBind();

    }


    protected void lvNavagation_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        int ID = DongDong.Common.Misc.ConvertToInt(e.CommandArgument);
        if (e.CommandName == "Return")
        {
            Nava objParentNavigation = Nava.Get(ID);
            Response.Redirect("GWManageNava.aspx?NavaParentID=" + (objParentNavigation == null ? "0" : objParentNavigation.ParentID.ToString()));

        }
    }

    public string NavagationTitleByID(object ID)
    {
        string Title = "没有父导航";
        Nava objNavagition = Nava.Get(DongDong.Common.Misc.ConvertToInt(ID));
        if (objNavagition != null)
            Title = objNavagition.Title;
        return Title;
    }

    protected void lbtnReturn_Click(object sender, EventArgs e)
    {
        if (NavaParentID != 0)
        {
            Nava objNa = Nava.Get(NavaParentID);
            if (objNa != null)
                Response.Redirect("GWManageNava.aspx?NavaParentID=" + objNa.ParentID);

        }
    }
}